<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@include file="WEB-INF/head/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <style>
        form{
            margin-top: 20px;
        }
        form div {
            margin-right: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <input type="hidden" name="msg" id="msg" value="${msg}">
            <form class="form-inline" action="/edoc/getAll" method="post">
                <div class="form-group">
                    <label>上传者</label>
                    <input type="text" class="form-control" id="username" name="username">
                </div>
                <div class="form-group">
                    <label>上传日期:</label>
                    <input type="date" class="form-control" name="star" >至
                    <input type="date" class="form-control" name="end" >
                </div>
                <div class="form-group">
                    <label>书籍类型:</label>
                    <select class="form-control" name="typeId" id="typeId">
                        <option value="0">请选择书籍类型</option>
                    </select>
                </div>
                <button type="submit" class="btn btn-default">查询</button>
            </form>
            <div style="margin-bottom: 20px">
                <a class="btn btn-default" href="/addEdoc.jsp" role="button">新增</a>
            </div>
        </div>
        <div class="col-md-10 col-md-offset-1">
            <table class="table table-hover table-bordered">
                <thead>
                    <tr>
                        <td>文档编号</td>
                        <td>文档名称</td>
                        <td>文档摘要</td>
                        <td>上传人</td>
                        <td>上传时间</td>
                        <td>操作</td>
                    </tr>
                </thead>

                <tbody>
                    <c:forEach items="${info.list}" var="edoc">
                        <tr>
                            <td>${edoc.EId}</td>
                            <td>${edoc.title}</td>
                            <td>${edoc.summary}</td>
                            <td>${edoc.user.username}</td>
                            <td><fmt:formatDate value="${edoc.createDate}" pattern="yyyy-MM-dd"></fmt:formatDate></td>
                            <td>
                                <button type="button" class="btn btn-danger" onclick="del(${edoc.EId})">删除</button>
                                <button type="button" class="btn btn-warning" onclick="updateEdoc(${edoc.EId})">修改</button>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>

                <tfoot>
                    <tr align="center">
                        <td colspan="8">
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    <li>
                                        <a href="/edoc/getAll?pageNum=1" aria-label="Previous">
                                            <span aria-hidden="true">首页</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="/edoc/getAll?pageNum=${info.prePage}" aria-label="Previous">
                                            <span aria-hidden="true">&laquo;</span>
                                        </a>
                                    </li>
                                    <c:forEach items="${info.navigatepageNums}" var="i">
                                        <li ${info.pageNum==i?"class='active'":""}><a href="/edoc/getAll?pageNum=${i}">${i}</a></li>
                                    </c:forEach>
                                    <li>
                                        <a href="/edoc/getAll?pageNum=${info.nextPage}" aria-label="Next">
                                            <span aria-hidden="true">&raquo;</span>
                                        </a>
                                    </li>

                                    <li>
                                        <a href="/edoc/getAll?pageNum=${info.pages}" aria-label="Next">
                                            <span aria-hidden="true">尾页</span>
                                        </a>
                                    </li>

                                </ul>
                            </nav>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

    </div>
</div>

<script>

    $(function () {
        var msg =$("#msg").val();
        if ("修改失败" == msg){
            alert(msg);
        } else if("修改成功" == msg){
            alert(msg);
        }else if("新增成功" == msg){
            alert(msg);
        }else if("新增失败" == msg){
            alert(msg);
        }


        $.ajax({
            url:"/edoc/getAllType",
            type:"get",
            dataType:"json",
            success:function (result) {
                for (let i in result){
                    $("#typeId").append("<option value='"+result[i].tId+"'>"+result[i].tName+"</option>")
                }
            }
        })
    })


    function updateEdoc(eId) {
        location.href="/edoc/getEdocByEId/"+eId;
    }


    function del(eId) {
        if(confirm("确定要删除吗？")){
            $.ajax({
                url:"/edoc/delEdoc/"+eId,
                type:"post",
                success:function (result) {
                    if(result==0){
                        alert("删除失败");
                    }else{
                        alert("删除成功");
                        location.reload();
                    }
                }
            })
        }
    }
    
</script>

</body>
</html>